using MicroMsg.Common.Algorithm;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Network;
using MicroMsg.Storage;
using System;
using System.IO.IsolatedStorage;
namespace MicroMsg.Scene.Video
{
	public class UploadVideoContext : IContextBase
	{
		public class ProgressInfo
		{
			public int totalLength;
			public int sentLength;
			public string clientMsgId;
			public int status;
			public void printInfo()
			{
				Log.smethod_5("UploadVideoContext", string.Concat(new object[]
				{
					" progress: totalLen = ",
					this.totalLength,
					",sentLen =",
					this.sentLength,
					", status = ",
					(MsgUIStatus)this.status,
					", clientmsgid = ",
					this.clientMsgId
				}));
			}
		}
		private const string TAG = "UploadVideoContext";
		public const int STATUS_INIT = 0;
		public const int STATUS_READY = 1;
		public const int STATUS_THUMBSENDING = 2;
		public const int STATUS_SENDING = 3;
		public const int STATUS_ERROR = 4;
		public const int STATUS_COMPLETE = 5;
		public MsgTrans mVideoTrans;
		private IsolatedStorageFileStream mThumbFileStream;
		private IsolatedStorageFileStream mVideoFileStream;
		public ChatMsg mChatMsg;
		public int mSendingLength;
		public NetSceneUploadVideo mScene;
		public UploadVideoContext.ProgressInfo mProgressInfo = new UploadVideoContext.ProgressInfo();
		public int mTotalLength
		{
			get
			{
				return this.mVideoTrans.nTotalDataLen;
			}
			set
			{
				this.mVideoTrans.nTotalDataLen = value;
			}
		}
		public int mNetOffset
		{
			get
			{
				return this.mVideoTrans.nTransDataLen;
			}
			set
			{
				this.mVideoTrans.nTransDataLen = value;
			}
		}
		public int mThumbTotalLength
		{
			get
			{
				return this.mVideoTrans.nRecordLength;
			}
			set
			{
				this.mVideoTrans.nRecordLength = value;
			}
		}
		public int mThumbNetOffset
		{
			get
			{
				return this.mVideoTrans.nEndFlag;
			}
			set
			{
				this.mVideoTrans.nEndFlag = value;
			}
		}
		public int mStatus
		{
			get
			{
				return this.mVideoTrans.nStatus;
			}
			set
			{
				this.mVideoTrans.nStatus = value;
			}
		}
		public void updateProgressInfo(int partLen)
		{
			this.mProgressInfo.totalLength = this.mTotalLength + this.mThumbTotalLength;
			this.mProgressInfo.sentLength = this.mNetOffset + this.mThumbNetOffset + partLen;
			this.mProgressInfo.clientMsgId = this.mVideoTrans.strClientMsgId;
			this.mProgressInfo.status = UploadVideoContext.convertStatus(this.mStatus);
			this.mProgressInfo.printInfo();
			EventCenter.postEvent(EventConst.ON_NETSCENE_UPLOADVIDEO_PROGRESS, this.mProgressInfo, null);
		}
		private UploadVideoContext()
		{
		}
		public static UploadVideoContext createByClientMsgID(string clientMsgID)
		{
			MsgTrans msgTrans = new MsgTrans();
			msgTrans.nTransType = 5;
			msgTrans.strClientMsgId = clientMsgID;
			return new UploadVideoContext
			{
				mVideoTrans = msgTrans
			};
		}
		public static UploadVideoContext createByMsgTrans(MsgTrans msgTrans)
		{
			return new UploadVideoContext
			{
				mVideoTrans = msgTrans
			};
		}
		public bool needToHandle()
		{
			return this.mStatus == 0;
		}
		public bool isRunning()
		{
			if (this.mStatus != 1 && this.mStatus != 3)
			{
				if (this.mStatus != 2)
				{
					return false;
				}
			}
			return true;
		}
		public bool needToClean()
		{
			return this.mStatus == 5 || this.mStatus == 4;
		}
		public bool isUploadCompleted()
		{
			return this.mThumbNetOffset == this.mThumbTotalLength && this.mNetOffset == this.mTotalLength;
		}
		public bool addVoiceChatMsg()
		{
			if (this.mChatMsg != null)
			{
				return false;
			}
			this.mChatMsg = new ChatMsg();
			this.mChatMsg.strMsg = "";
			this.mChatMsg.strTalker = this.mVideoTrans.strToUserName;
			this.mChatMsg.nMsgType = 43;
			this.mChatMsg.nStatus = UploadVideoContext.convertStatus(this.mStatus);
			this.mChatMsg.nCreateTime = (long)(Util.getNowMilliseconds() / 1000.0);
			this.mChatMsg.nIsSender = 1;
			this.mChatMsg.strClientMsgId = this.mVideoTrans.strClientMsgId;
			this.mChatMsg.strThumbnail = this.mVideoTrans.strThumbnail;
			this.mChatMsg.strPath = this.mVideoTrans.strImagePath;
			this.updateChatMsgContent();
			StorageMgr.chatMsg.addMsg(this.mChatMsg);
			this.mVideoTrans.nMsgLocalID = this.mChatMsg.nMsgLocalID;
			this.updateToVideoTransMsg();
			return true;
		}
		public void updateToVideoTransMsg()
		{
			Log.smethod_5("UploadVideoContext", "update to trans storage..");
			StorageMgr.msgVideo.update(this.mVideoTrans);
		}
		public void updateToVoiceChatMsg()
		{
			this.mChatMsg.nStatus = UploadVideoContext.convertStatus(this.mStatus);
			this.mChatMsg.nMsgSvrID = this.mVideoTrans.nMsgSvrID;
			this.updateChatMsgContent();
			StorageMgr.chatMsg.modifyMsg(this.mChatMsg);
		}
		public static int convertStatus(int status)
		{
			MsgUIStatus result = MsgUIStatus.Processing;
			if (status == 5)
			{
				result = MsgUIStatus.Success;
			}
			else
			{
				if (status == 4)
				{
					result = MsgUIStatus.Fail;
				}
			}
			return (int)result;
		}
		private void updateChatMsgContent()
		{
			this.mChatMsg.strMsg = string.Concat(new object[]
			{
				"<msg><videomsg  playlength=\"",
				this.mVideoTrans.nDuration,
				"\" length=\"",
				this.mTotalLength,
				"\" clientmsgid=\"",
				this.mVideoTrans.strClientMsgId,
				"\" /></msg>"
			});
		}
		public void onFinished()
		{
			if (this.mThumbFileStream != null)
			{
				this.mThumbFileStream.Close();
				this.mThumbFileStream = null;
			}
			if (this.mVideoFileStream != null)
			{
				this.mVideoFileStream.Close();
				this.mVideoFileStream = null;
			}
			this.updateToVoiceChatMsg();
		}
		public bool updateContext()
		{
			if (this.mStatus == 5)
			{
				Log.smethod_5("UploadVideoContext", "delete  upload video context, All completed.");
				StorageMgr.msgVideo.delByTransID(this.mVideoTrans.nMsgTransID);
			}
			else
			{
				this.updateToVideoTransMsg();
			}
			return true;
		}
		public void preProcessVideoFile(string thumbFile, string videoFile)
		{
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					if (!userStoreForApplication.DirectoryExists(StorageIO.getVideoPath()))
					{
						userStoreForApplication.CreateDirectory(StorageIO.getVideoPath());
					}
					string hashString = MD5Core.GetHashString(thumbFile + Util.getNowMilliseconds());
					string text = StorageIO.getVideoPath() + "/upvideothumb" + hashString + ".jpg";
					userStoreForApplication.CopyFile(thumbFile, text);
					this.mVideoTrans.strThumbnail = text;
					hashString = MD5Core.GetHashString(videoFile + Util.getNowMilliseconds());
					text = StorageIO.getVideoPath() + "/uploadvideo" + hashString + ".mp4";
					userStoreForApplication.CopyFile(videoFile, text);
					this.mVideoTrans.strImagePath = text;
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("UploadVideoContext", ex.get_Message());
			}
		}
		public bool initThumbFileStream()
		{
			if (this.mThumbFileStream != null)
			{
				return true;
			}
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					this.mThumbFileStream = userStoreForApplication.OpenFile(this.mVideoTrans.strThumbnail, 3, 1);
					bool result;
					if (this.mThumbFileStream == null)
					{
						result = false;
						return result;
					}
					this.mThumbTotalLength = (int)this.mThumbFileStream.get_Length();
					result = true;
					return result;
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("UploadVideoContext", ex.get_Message());
			}
			return false;
		}
		public bool initVideoFileStream()
		{
			if (this.mVideoFileStream != null)
			{
				return true;
			}
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					this.mVideoFileStream = userStoreForApplication.OpenFile(this.mVideoTrans.strImagePath, 3, 1);
					bool result;
					if (this.mVideoFileStream == null)
					{
						result = false;
						return result;
					}
					this.mTotalLength = (int)this.mVideoFileStream.get_Length();
					result = true;
					return result;
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("UploadVideoContext", ex.get_Message());
			}
			return false;
		}
		public byte[] getRemainThumbToSend()
		{
			if (!this.initThumbFileStream())
			{
				return null;
			}
			if (this.mThumbNetOffset < 0)
			{
				return null;
			}
			if (this.mThumbTotalLength <= 0)
			{
				return null;
			}
			if (this.mThumbNetOffset >= this.mThumbTotalLength)
			{
				return null;
			}
			int num = this.getSendPackSize();
			if (this.mThumbTotalLength - this.mThumbNetOffset < num)
			{
				num = this.mThumbTotalLength - this.mThumbNetOffset;
			}
			byte[] array = new byte[num];
			this.mThumbFileStream.Seek((long)this.mThumbNetOffset, 0);
			int num2 = this.mThumbFileStream.Read(array, 0, num);
			if (num2 != num)
			{
				return null;
			}
			return array;
		}
		public byte[] getRemainVideoToSend()
		{
			if (!this.initVideoFileStream())
			{
				return null;
			}
			if (this.mNetOffset < 0)
			{
				return null;
			}
			if (this.mTotalLength <= 0)
			{
				return null;
			}
			if (this.mNetOffset >= this.mTotalLength)
			{
				return null;
			}
			int num = this.getSendPackSize();
			if (this.mTotalLength - this.mNetOffset < num)
			{
				num = this.mTotalLength - this.mNetOffset;
			}
			byte[] array = new byte[num];
			this.mVideoFileStream.Seek((long)this.mNetOffset, 0);
			int num2 = this.mVideoFileStream.Read(array, 0, num);
			if (num2 != num)
			{
				return null;
			}
			return array;
		}
		public int getSendPackSize()
		{
			if (NetworkDeviceWatcher.isCurrentWifi())
			{
				return 32768;
			}
			if (NetworkDeviceWatcher.isCurrentCell3G())
			{
				return 16384;
			}
			return 8192;
		}
		public bool loadFromMsgTrans()
		{
			if (!this.initThumbFileStream())
			{
				Log.smethod_1("UploadVideoContext", "load from thumb failed , no data found ");
				this.mStatus = 5;
				this.updateContext();
				return false;
			}
			if (!this.initVideoFileStream())
			{
				Log.smethod_1("UploadVideoContext", "load from video failed , no data found ");
				this.mStatus = 5;
				this.updateContext();
				return false;
			}
			this.mChatMsg = StorageMgr.chatMsg.getMsg(this.mVideoTrans.nMsgLocalID);
			if (this.mChatMsg == null)
			{
				Log.smethod_1("UploadVideoContext", "failed to load chat msg by clientmsgid , ignored task. ");
				this.mStatus = 5;
				this.updateContext();
				return false;
			}
			Log.smethod_1("UploadVideoContext", "load video from msgtrans success. ");
			this.mStatus = 0;
			this.updateToVoiceChatMsg();
			return true;
		}
	}
}
